package config

import (
	"gopkg.in/yaml.v3"
	"log"
	"os"
	"path/filepath"
	"runtime"
)

func Config() (string, string) {
	relativePath := "config.yaml"

	_, filename, _, _ := runtime.Caller(0)
	absPath, err := filepath.Abs(filename)
	if err != nil {
		log.Printf("转换为绝对路径失败: %v\n", err)
		return "", ""
	}
	// 获取目录路径
	dir := filepath.Dir(absPath)
	return dir, relativePath
}

type LogGer struct {
	LogFile string `yaml:"LogFile"`
}

type Mail struct {
	// 发件人信息
	SenderEmail    string `yaml:"SenderEmail"`
	SenderPassword string `yaml:"SenderPassword"`
	// SMTP服务器地址和端口
	SmtpServer string `yaml:"SmtpServer"`
	//收件人邮箱
	ReceiverEmail string `yaml:"ReceiverEmail"`
	// 邮件主题和正文
	Subject  string   `yaml:"Subject"`
	Body     string   `yaml:"Body"`
	CcEmails []string `yaml:"CcEmails"`
}

type DingDing struct {
	WebhookURL string `yaml:"WebhookURL"`
	Secret     string `yaml:"Secret"`
	Message    string `yaml:"Message"`
}

type Sgip struct {
	LoginName     string   `yaml:"LoginName"`
	LoginPassword string   `yaml:"LoginPassword"`
	SmgIpPort     string   `yaml:"SmgIpPort"`
	SPNumber      string   `yaml:"SPNumber"`
	ChargeNumber  string   `yaml:"ChargeNumber"`
	UserNumber    []string `yaml:"UserNumber"`
	CorpID        string   `yaml:"CorpID"`
	ServiceType   string   `yaml:"ServiceType"`
	Message       string   `yaml:"Message"`
}

type SMS struct {
	Type    int8     `yaml:"Type"`
	Command []string `yaml:"Command"`
	Sgip    Sgip     `yaml:"Sgip"`
}

type ContentSpecifications struct {
	Type    int    `yaml:"Type"`
	File    string `yaml:"File"`
	Content string `yaml:"Content"`
}

type StatusAny struct {
	Mail     int8 `yaml:"Mail"`
	DingDing int8 `yaml:"DingDing"`
	SMS      int8 `yaml:"SMS"`
}

type BeforeAndAfterCommands struct {
	BeforeStatus        int8     `yaml:"BeforeStatus"`
	BeforeStatusCommand []string `yaml:"BeforeStatusCommand"`
	AfterStatus         int8     `yaml:"AfterStatus"`
	AfterCommand        []string `yaml:"AfterCommand"`
}

type PushConfig struct {
	Mail                   Mail                   `yaml:"Mail"`
	DingDing               DingDing               `yaml:"DingDing"`
	SMS                    SMS                    `yaml:"SMS"`
	ContentS               ContentSpecifications  `yaml:"ContentS"`
	Status                 StatusAny              `yaml:"Status"`
	BeforeAndAfterCommands BeforeAndAfterCommands `yaml:"BeforeAndAfterCommands"`
	LogGer                 LogGer                 `yaml:"LogGer"`
}

// 实现接口的结构体
type DefaultMailConfigProvider struct{}

func (d *DefaultMailConfigProvider) GetMailConfig() PushConfig {
	FilePath := filepath.Join(Config())
	YamlFile, err := os.Open(FilePath)
	if err != nil {
		log.Printf("配置文件打开失败: %v\n", err)
		return PushConfig{}
	}
	defer YamlFile.Close()

	// 解析yaml文件内容到MailConfig
	var YamlContent PushConfig
	err = yaml.NewDecoder(YamlFile).Decode(&YamlContent)
	if err != nil {
		log.Printf("配置文件解析失败: %v\n", err)
		return PushConfig{}
	}
	// 这里可以根据实际情况初始化 MailConfig
	return YamlContent
}
